<?php

/*
 *  Copyright (C) 2012 etpass
 * 
 *  author:chl <chenhl, chenhl@etpass.com>
 *  date:2012-8-6
 *  encoding:UTF-8
 *  filename:functions.php
 *  link:http://www.etpass.com
 */

function __autoload($classname) {
    // 自动加载当前项目的controller类和Model类
    if (substr($classname, -6) == 'Module') {
        require_rewrite(APP_MODULE_PATH . $classname . '.class.php');
//         require_rewrite(GROUP_COMMON_MODULE_PATH . $classname . '.class.php');
        require_rewrite(COMMON_MODULE_MODULE_PATH . $classname . '.class.php');//自动加载 Module
    } elseif (substr($classname, -7) == 'Manager') {
        require_rewrite(APP_MANAGER_PATH . $classname . '.class.php');//自动加载 Manager下的类
    }elseif (substr($classname, -3) == 'Dao') {
        require_rewrite(COMMON_MODULE_DAO_PATH . $classname . '.class.php');//自动加载 Dao下的类
    } elseif (substr($classname, -10) == 'Controller') {
        require_rewrite(COMMON_CONTROLLER_PATH . $classname . '.class.php');//加载项目下的controller
        require_rewrite(APP_CONTROLLER_PATH . $classname . '.class.php');//加载项目下的controller
        require_rewrite(GROUP_COMMON_CONTROLLER_PATH . $classname . '.class.php');//加载同类型公共的controller 如base HotelService MemberService等
    } else {
        // 根据自动加载路径设置进行尝试搜索
        require_rewrite(AUTO_LOAD_PATH . $classname . '.class.php');
    }
    return;
}

// require_once
function require_rewrite($filename) {
    static $_importFiles = array();
    $filename = realpath($filename);
    if (!isset($_importFiles[$filename])) {
        if (is_file($filename)) {
            require $filename;
            $_importFiles[$filename] = true;
        } else {
            $_importFiles[$filename] = false;
        }
    }
    return $_importFiles[$filename];
}

function url($url) {

    $return = '';
    $url_info = parse_url($url);
    if ($url_info['scheme'] == 'https') {
        
    }
    if ($url_info['path']) {
        
    }
    if ($url_info['query']) {
        parse_str($url_info['query'], $query);
        $c = $query['c'];
        $m = $query['m'];
        unset($query['c'], $query['m']);

        if ($query)
            $param = '?' . http_build_query($query);
        else
            $param = '';

        $return .= '/' . $c . '/' . $m . '/' . $param;
    }
    if ($return)
        return $return;
    else
        return $url;
}

/**
 * 安全过滤
 *
 * @param str $string
 * @return str
 */
function safe_replace($string) {
    $string = str_replace('%20', '', $string);
    $string = str_replace('%27', '', $string);
    $string = str_replace('%2527', '', $string);
    $string = str_replace('*', '', $string);
    $string = str_replace('"', '&quot;', $string);
    $string = str_replace("'", '', $string);
    $string = str_replace('"', '', $string);
    $string = str_replace(';', '', $string);
    $string = str_replace('<', '&lt;', $string);
    $string = str_replace('>', '&gt;', $string);
    $string = str_replace("{", '', $string);
    $string = str_replace('}', '', $string);
    $string = str_replace('\\', '', $string);


    return $string;
}
//$string = str_replace(array(' ','&nbsp;', '&amp;', '&quot;', '&#039;', '&ldquo;', '&rdquo;', '&mdash;', '&lt;', '&gt;', '&middot;', '&hellip;'), array('∵',' ', '&', '"', "'", '“', '”', '—', '<', '>', '·', '…'), $string);
//$strcut = str_replace(array('∵', '&', '"', "'", '“', '”', '—', '<', '>', '·', '…'), array(' ', '&amp;', '&quot;', '&#039;', '&ldquo;', '&rdquo;', '&mdash;', '&lt;', '&gt;', '&middot;', '&hellip;'), $strcut);
/**
 * _GET,_POST,_COOKIE等的过滤 
 * @param str $string
 * @return str 
 */
function etpass_replace($string) {
    
    $string = preg_replace("/select|insert|update|delete|union|into|load_file|outfile|\*| and | or /i", " ", $string);
    //过滤html标签
    
    $new_string = filter_var($string, FILTER_SANITIZE_STRING);
    return $new_string;
    if (false !== $new_string) {
        $string = $new_string;
    }
    return htmlspecialchars($string);
//    return $string;
}

/**
 * 获取当前页面完整URL地址
 */
function get_url() {
    $sys_protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
    $php_self = $_SERVER['PHP_SELF'] ? safe_replace($_SERVER['PHP_SELF']) : safe_replace($_SERVER['SCRIPT_NAME']);
    $path_info = isset($_SERVER['PATH_INFO']) ? safe_replace($_SERVER['PATH_INFO']) : '';
    $relate_url = isset($_SERVER['REQUEST_URI']) ? safe_replace($_SERVER['REQUEST_URI']) : $php_self . (isset($_SERVER['QUERY_STRING']) ? '?' . safe_replace($_SERVER['QUERY_STRING']) : $path_info);
    return $sys_protocal . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '') . $relate_url;
}

function get_referer_url(){
    return safe_replace($_SERVER['HTTP_REFERER']);
}
/**
 * 酒店距离单位转换，不是单纯的单位转换（小于100米直接显示0.1公里）。
 * 
 * @param type $m
 * @return type 
 */
function m2km($m) {      
    return $m>100?((int)($m/100)*100)/1000:0.1;//大于100m 去尾 转换成公里，否则显示0.1km
}